package com.simple.datax.dao.impl;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.datax.dao.IDataxFlowDAO;
import com.simple.datax.vo.DataxFlowIdVO;
import com.simple.datax.vo.DataxFlowVO;
import com.simple.util.common.StringUtil;
import com.simple.util.object.PageInfoBean;

@Repository("dataxFlowDAO")
public class DataxFlowDAOImpl extends BaseDAO implements
		IDataxFlowDAO {

	@Override
	public DataxFlowVO findById(DataxFlowIdVO id) {
		return super.get(DataxFlowVO.class, id);
	}

	@Override
	public void saveOrUpdate(DataxFlowVO dataxFlowVO) {
		super.saveOrUpdate(dataxFlowVO);
	}

	@Override
	public void update(DataxFlowVO dataxFlowVO) {
		super.saveOrUpdate(dataxFlowVO);
	}

	@Override
	public List<DataxFlowVO> findAll() {
		return super.query(DataxFlowVO.class);
	}

	@Override
	public void delete(DataxFlowVO dataxFlowVO) {
		super.delete(dataxFlowVO);
	}

	@Override
	public void delete(DataxFlowIdVO id) {
		super.delete(DataxFlowVO.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DataxFlowVO> findDataxFlowVOByPage(PageInfoBean pageInfoBean,
			String flowId, String originateCop, String ecName,String status) {
		final Map<String, Object> paramMap = new LinkedHashMap<String,Object>();
        final StringBuffer sb = new StringBuffer(500);
        
        if(!StringUtil.isNull(ecName)){
        	sb.append( "select  d from DataxFlowVO d, VwDataxEnterpriseVO e where d.originateCop=e.ecCode " );
        	sb.append( " and e.ecName like:ecName" );
            paramMap.put("ecName", "%"+ecName+"%");
         }else{
        	 sb.append( "select  d from DataxFlowVO d where 1>0 ");
         }
        if(!StringUtil.isNull(flowId)){
            sb.append( " and d.id.flowId like:id " );
            paramMap.put("id","%"+flowId+"%");
        }
        if(!StringUtil.isNull(originateCop)){
            sb.append( " and d.originateCop like:originateCop " );
            paramMap.put("originateCop","%"+ originateCop+"%");
        }
        if(!StringUtil.isNull(status)){
        	sb.append( " and d.status =:status " );
        	paramMap.put("status",BigDecimal.valueOf(Double.valueOf(status)));
        } 

        sb.append( " order by d.id.flowId desc " );
        
		return (List<DataxFlowVO>)this.queryListByPage("d.id.flowId", sb.toString(), paramMap, pageInfoBean);
	}

	@Override
	public String generateId() {
			List<?> list =  getSession().createSQLQuery("select SEQ_DATAX_FLOW_ID.nextval from dual").list();
			return (list.get(0)).toString();
	}

	@Override
	public boolean checkExistFlowName(String flowName) {
		String hql=" from DataxFlowVO d where 1>0 and d.flowName=? ";
		return this.query(hql, flowName).size()>0;
	}

}
